const merchantFacade = require('../../app/facade/merchant.facade.js');
const _ = require('lodash');
var request = require('request');

module.exports = {
    merchantRegister: merchantRegister,
    getMerchantByLogin: getMerchantByLogin,
    getMerchantByIcnum: getMerchantByIcnum,
    merchantUpdate: merchantUpdate,
    merchantUpdateFromA: merchantUpdateFromA,
    getMerchantStatus2ByIcnum:getMerchantStatus2ByIcnum,
    openMerchant: openMerchant,
    closeMerchant: closeMerchant
}



function merchantRegister(req, res, next) {
    let merchant = req.body;
    let files = req.files;

    // console.log(merchant);
    console.log(files);
    merchantFacade.merchantRegister(merchant, files, (error, result) => {
        console.log('after insert:');
        console.log(result);
        if (error) {
            return next(new Error(error));
        } else {

            res.redirect("/#!/login");
        }
    });
}

function getMerchantByLogin(req, res, next) {
    let merchant = req.body;
    console.log(merchant);
    merchantFacade.getMerchantByLogin(merchant, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            console.log(result+"788888");
            var status1 = result.status1;
            console.log(result);
            console.log(status1);
            if (status1 == "1") {
                return res.json(result);
                
            } else {
                getStatus(result.icnum,result,function(data,result){
                 if(data.status=="1"){
                     result.status1 ='1';
                     console.log(result);
                 }
                    var resData = {};
                    resData.m = result;
                    resData.data = data;
                    console.log("resData**********************************************");
                    console.log(resData);
                     res.json(resData);
                });
            }
        }
    });
}

function getStatus(icnum,result,callback) {
    request('http://10.222.29.185:8999/a/merchant/check/'+icnum, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
            var data = JSON.parse(body);
            console.log(data.icnum);
            merchantFacade.updateMerchantStatus1(data);
            callback(data,result);
        }
    })



}


function getMerchantByIcnum(req, res, next) {
    let params = req.params;
    let icnum = params.icnum;
    merchantFacade.getMerchantByIcnum(icnum, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });

}

function getMerchantStatus2ByIcnum(req, res, next) {
    let params = req.params;
    let icnum = params.icnum;
    merchantFacade.getMerchantByIcnum(icnum, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result.status2);
        }
    });

}


function merchantUpdate(req, res, next) {
    let merchant = req.body;
     let files = req.files;
    merchantFacade.merchantUpdate(merchant,files, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
             res.redirect("/#!/login");
        }
    });
}

function merchantUpdateFromA(req, res, next) {
    let merchant = req.body;
    merchantFacade.merchantUpdateStatus2(merchant, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });


}

function openMerchant(req, res, next){
    let merchant = req.body;
    merchantFacade.openMerchant(merchant, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
}

function closeMerchant(req, res, next){
    let merchant = req.body;
    merchantFacade.closeMerchant(merchant, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
}
